package cn.edu.bjut.explosion;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import cn.edu.bjut.explosion.Http.HttpUtils;
import cn.edu.bjut.explosion.JsonGson.LoginJson;

public class LoginActivity extends AppCompatActivity {

    private EditText l_uid;
    private EditText l_password;
    private String username;
    private String password;
    private String strResult;
    private String result;

    String url2 = "http://dewusang.xin:8080/api/login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_login);

        l_uid = findViewById(R.id.l_uid);
        l_password = findViewById(R.id.l_pw);

        //下面的这个是直接不让你输入空格了，我觉得有点暴力。。看看换个方法吧，毕竟密码输入空格也没啥
        l_uid.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(" ")) {
                    //下面这个就是强行去除空格的鬼东西。。。烦躁
                    String[] str = charSequence.toString().split(" ");
                    String str1 = "";
                    for (int x = 0; x < str.length; x++) {
                        str1 += str[x];
                    }
                    l_uid.setText(str1);
                    l_uid.setError("にたま的别输入空格了！");
                    l_uid.setSelection(i);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        l_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(" ")) {
                    //下面这个就是强行去除空格的鬼东西。。。烦躁
                    String[] str = charSequence.toString().split(" ");
                    String str1 = "";
                    for (int x = 0; x < str.length; x++) {
                        str1 += str[x];
                    }
                    l_password.setText(str1);
                    l_password.setError("にたま的别输入空格了！");
                    l_password.setSelection(i);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    public void login(View view) throws InterruptedException {

        username = l_uid.getText().toString();
        password = l_password.getText().toString();
        Log.d("username",username);
        Log.d("password",password);
        if(username.equals("")) {
            Toast.makeText(LoginActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
        } else {
            if(password.equals("")) {
                Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
            } else {
                panduan();
                if(result.equals("Login success!")) {
                    Toast.makeText(LoginActivity.this,"いらっしゃいませ~Master",Toast.LENGTH_LONG).show();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                }
            }
        }

        //这个是分割线


    }

    public void register(View view){
        startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        finish();
    }

    public void facelongin(View view){
        Toast.makeText(LoginActivity.this,"我还没做呐！！！别点了",Toast.LENGTH_LONG).show();
    }

    public void panduan() throws InterruptedException {
        Thread logthread = new Thread() {
            @Override
            public void run() {
                super.run();
                try {

                    //提交
                    RecSmsToPost(username,password);
                    Gson gson = new Gson();
                    LoginJson bean = gson.fromJson(strResult,LoginJson.class);
                    result = bean.getResult();//从处理好的数据中取出所需的数值
                    Log.d("result1",result);


                } catch (Exception e) {
                }

            }

            private void RecSmsToPost(String username,String password) {

                //参数
                Map<String,String> params = new HashMap<String,String>();
                params.put("username", username);
                params.put("password",password);


                //服务器请求路径
                String strUrlPath = url2;
                strResult= HttpUtils.submitPostData(strUrlPath,params, "utf-8");

                Log.d("oraoraoraora:",strResult);
                //openToast("提交完成");

            }
        };
        logthread.start();
        logthread.join();
        logthread.interrupt();
        logthread = null;

    }

}
